import {ref} from 'vue'
import {burst, blobString} from '../utils/fileBurst'
import sha1 from 'js-sha1'
import fileUpload from './fileUpload'

export default function fileHandler(){
    const burstObj = ref({burst:[]})

    const fileChange = async function(e){
        const file = e.currentTarget.files[0]
        burstObj.value.file = {
            file,
            hash: sha1(file)
        }
        burstObj.value.burst = await Promise.all(burst(file, 1024 * 200).map(async (blob, i)=>{
            var text =  await blobString(blob)
            return {blob, i, hash: sha1(text), text, press: 0}
        }))
        fileUpload(burstObj)
        console.log(burstObj.value)
    }
    return {fileChange, burstObj}
}